#pragma once
#include <jsoncpp/json/json.h>
#include <signal.h>
#include "compiler.hpp"
#include "runner.hpp"
#include "../comm/log.hpp"
#include "../comm/util.hpp"

namespace ns_compiler_and_run
{
    using namespace ns_compiler;
    using namespace ns_runner;
    using namespace ns_util;
    using namespace ns_log;

    class compile_and_run
    {
        static std::string CodeToDesc(int code, const std::string &file_name)
        {
            std::string desc;
            switch (code)
            {
            case 0:
                desc = "编译运行成功";
                break;
            case -1:
                desc = "传入代码为空";
                break;
            case -2:
                desc = "未知错误";
                break;
            case -3:
                {
                    std::string read_compile_err;
                    FileUtil::ReadFile(PathUtil::CompileErr(file_name), &read_compile_err, true);
                    desc = read_compile_err;
                }
                break;
            case SIGABRT:
                desc = "超出内存限制";
                break;
            case SIGXCPU:
                desc = "超出时间限制";
                break;
            default:
                desc = "未知: " + std::to_string(code);
                break;
            }
            return desc;
        }

        static void RemoveTempFile(const std::string& file_name)
        {
            std::string _src = PathUtil::Src(file_name);
            if(FileUtil::IsFileExists(_src)) unlink(_src.c_str());

            std::string _compileerr = PathUtil::CompileErr(file_name);
            if(FileUtil::IsFileExists(_compileerr)) unlink(_compileerr.c_str());

            std::string _exe = PathUtil::Exe(file_name);
            if(FileUtil::IsFileExists(_exe)) unlink(_exe.c_str());

            std::string _stdout = PathUtil::Stdout(file_name);
            if(FileUtil::IsFileExists(_stdout)) unlink(_stdout.c_str());

            std::string _stdin = PathUtil::Stdin(file_name);
            if(FileUtil::IsFileExists(_stdin)) unlink(_stdin.c_str());

            std::string _stderr = PathUtil::Stderr(file_name);
            if(FileUtil::IsFileExists(_stderr)) unlink(_stderr.c_str());
        }

    public:
        /********************
         * 输入
         *  code:代码
         *  input:输入值
         *  cpu_limit:时间
         *  mem_limit:空间
         *
         **********************/

        static void start(const std::string &_in_json, std::string *_out_json)
        {

            Json::Value in_json;
            Json::Reader reader;
            reader.parse(_in_json, in_json);

            std::string code = in_json["code"].asString();
            std::string input = in_json["input"].asString();
            int cpu_limit = in_json["cpu_limit"].asInt();
            int mem_limit = in_json["mem_limit"].asInt();
            int status_code = 0;
            std::string file_name;
            int run_result = 0;

            Json::Value out_json;

            if (code.size() == 0)
            {
                status_code = -1;
                goto END;
            }

            file_name = FileUtil::UniqFileName();

            if (!FileUtil::WriteCode(PathUtil::Src(file_name), code))
            {
                status_code = -2;
                goto END;
            }

            if (!compiler::compile(file_name))
            {
                status_code = -3;
                goto END;
            }

            run_result = runner::run(file_name, cpu_limit, mem_limit);
            if (run_result < 0)
            {
                status_code = -2;
            }
            else if (run_result > 0)
            {
                status_code = run_result;
            }

        END:
            out_json["status"] = status_code;
            out_json["reason"] = CodeToDesc(status_code, file_name);
            if (status_code == 0)
            {
                std::string read_out;
                FileUtil::ReadFile(PathUtil::Stdout(file_name), &read_out,true);
                out_json["stdout"] = read_out;

                std::string read_err;
                FileUtil::ReadFile(PathUtil::Stderr(file_name), &read_err,true);
                out_json["stderr"] = read_err;
            }
            Json::StyledWriter writer;
            *_out_json = writer.write(out_json);
            RemoveTempFile(file_name);
        }
    };
}
